﻿package com.edward.service;


import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Item;


public class ItemManager extends HibernateDaoSupport {

	public void addItem(Item item) {
		
		getHibernateTemplate().save(item);
	}
	
	public void updateItem(Item item) {
		
		getHibernateTemplate().update(item);
	}
	
	
	public Item getItem(int id){
		Item d =(Item)getHibernateTemplate().get(Item.class, id);
		return d;
	}

	public void deleteItem(int id){
		
		getHibernateTemplate().delete(getItem(id));
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerItem(int start,int limit,int id) {
		List <Item>list = getHibernateTemplate().find("from Item d where ITEMS_ID = " + id );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Item>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}


}



